<?php
include '../../../wp-blog-header.php';
$brand_id = '906';

$url = 'http://chandigarh.couponsonmobile.com/deal/rss?active&tracking_id=4d9b1bee346d9';

function download_page($path){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$path);
        curl_setopt($ch, CURLOPT_FAILONERROR,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        $retValue = curl_exec($ch);                      
        curl_close($ch);
        return $retValue;
}


  $curl_json_output = download_page($url);
  $json_decoded = json_decode($curl_json_output, true);
  $akc_deals = xml2array($curl_json_output);
  $akc_daily_deals = $akc_deals['rss']['channel']['item'];
 

//echo "<pre>";
//print_r($akc_daily_deals);
// exit();

foreach($akc_daily_deals as $city_key => $city_value){
    //foreach($city_value as $key => $value){
    
    $columns_deal['name'] = pods_sanitize($city_value['title']);
    $columns_deal['camp_link'] = sanitize_title($columns_deal['name']);
    $ext_img_url = $city_value['enclosure_attr']['url'];
     
    $deal = new Pod('deal');
    $where = "t.camp_link = '".$columns_deal['camp_link']."' AND t.daily = 1";
    $deal->findRecords('id DESC', 1,$where);
    $dealsFound = $deal->getTotalRows();
    //echo $dealsFound; 
    
    if($dealsFound == 0)
    {
      unset($city_id);
      $City = new Pod('city');
    	$City->findRecords('name ASC', -1,"t.name='Chandigarh'");
    	while($City->fetchRecord())
    	{
      	$city_id  = $City->get_field('id');
    	}
      $columns_deal['city'] = $city_id;
      
      $shortened_url = make_bitly_url($city_value['link']);
      if(empty($shortened_url))
      {
        $shortened_url = make_goog_url($city_value['link']);
      }
      $columns_deal['extlink'] = $shortened_url;

      $columns_deal['camp_end'] = $city_value['COM:endlistdate'];
      $columns_deal['camp_start'] = $city_value['COM:listdate']; 
      $columns_deal['brand'] = $brand_id;
      $columns_deal['daily'] = '1';
      $columns_deal['oriprice'] = floor($city_value['COM:value']);
      $columns_deal['disper'] = floor($city_value['COM:savings']);
      $columns_deal['disprice'] = floor($city_value['COM:price']);
      $columns_deal['ext_image_url'] = $ext_img_url;
    
      $cat_id = check_daily_category($columns_deal['name']);
      if(!empty($cat_id))
      {
        $columns_deal['camp_cat'] = $cat_id;
      }
    
      $api = new PodAPI();
      $params = array('datatype' => 'deal', 'columns' => $columns_deal);
      
      
    // echo '<pre>';
     // print_r($params);
    //  echo '</pre>';
      //exit();
      
  $api->save_pod_item($params);
  }

 //}
}
?>